package com.example.yml0528;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yml.R;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText name;
    private EditText pw;
    private CheckBox jz;
    private TextView wj;
    private TextView zc;
    private Button login;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
    }

    public void init() {
        name = findViewById(R.id.name);
        pw = findViewById(R.id.pw);
        jz = findViewById(R.id.jz);
        wj = findViewById(R.id.wj);
        zc = findViewById(R.id.zc);
        login = findViewById(R.id.login);
        wj.setOnClickListener(this);
        zc.setOnClickListener(this);
        login.setOnClickListener(this);
        user = new User();
        user.setUsername("admin");
        user.setPw("123456");
        user.setSex("男");
        user.setHobby("打游戏");
        user.setPhone("12345678998");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 111) {
            user = (User) data.getSerializableExtra("myuser");
            name.setText(user.getUsername());
            pw.setText(user.getPw());
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.wj:
                break;
            case R.id.zc:
                Intent intent = new Intent(LoginActivity.this, RegistActivity.class);
                startActivityForResult(intent, 111);
                break;
            case R.id.login:
                String uname = name.getText().toString();
                String upw = pw.getText().toString();
                if (TextUtils.isEmpty(uname) || TextUtils.isEmpty(upw)) {
                    Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
                } else {
                    if (jz.isChecked()) {
                        if (user != null) {
                            if (uname.equals(user.getUsername()) && upw.equals(user.getPw())) {
                                Intent intent1 = new Intent(LoginActivity.this,MainActivity.class);
                                intent1.putExtra("user", user);
                                startActivity(intent1);
                            } else {
                                Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            Toast.makeText(this, "该用户未注册", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "请勾选记住密码", Toast.LENGTH_SHORT).show();
                    }
                }
                break;

        }
    }
}
